package com.zzptc.lxy.mybook.ui.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.core.content.SharedPreferencesCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.zzptc.lxy.mybook.Adapter.BookAdapter;
import com.zzptc.lxy.mybook.Adapter.CollectionAdapter;
import com.zzptc.lxy.mybook.Bean.Book;
import com.zzptc.lxy.mybook.Bean.ChooseBook;
import com.zzptc.lxy.mybook.Bean.CollectionItem;
import com.zzptc.lxy.mybook.R;
import com.zzptc.lxy.mybook.ReadActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private ListView booklv;
    List<Book> bookList = new ArrayList<>();
    List<ChooseBook> chooseBookList = new ArrayList<>();
    List<CollectionItem> collectionItemList = new ArrayList<>();
    CollectionAdapter adapter;
//主页面
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initData();
        initView(view);
        setListener();
        return view;
    }

    private void initData() {
        SharedPreferences sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        int uid = sp.getInt("userid", -1);
        chooseBookList = LitePal.where("userid=?", uid + "").find(ChooseBook.class);
        bookList = LitePal.findAll(Book.class);
        for (Book b : bookList) {
            Boolean k = false;
            for (ChooseBook c : chooseBookList) {
                if (b.getId() == c.getBookid()) {
                    k = true;
                }
            }
            CollectionItem item = new CollectionItem(b.getBookimg(), b.getBookname(), b.getAuthorname(), k);
            collectionItemList.add(item);
        }
    }

    private void setListener() {
//        点击阅读   跳转到阅读页面
        booklv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it = new Intent(getContext(), ReadActivity.class);
                it.putExtra("bookimg", collectionItemList.get(position).getBookimg());
                it.putExtra("bookname", collectionItemList.get(position).getBookname());
                it.putExtra("authorname", collectionItemList.get(position).getAuthorname());
                startActivity(it);
            }
        });
//长按收藏
        booklv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                int uid = sp.getInt("userid", -1);
                int bid = bookList.get(position).getId();
                if (isCollected(uid, bid)) {
                    new ChooseBook(0, uid, bid).save();
                    collectionItemList.get(position).setIscollected(true);
                    adapter.notifyDataSetChanged();
                    showToast("收藏成功");
                }
                test();
                return true;
            }
        });
    }

    private void test() {
        for (ChooseBook c : LitePal.findAll(ChooseBook.class)) {
            Log.e("choosebook", c.getId() + " " + c.getUserid() + " " + c.getBookid());
        }
    }

    private void showToast(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

    private boolean isCollected(int uid, int bid) {
        for (ChooseBook c : LitePal.findAll(ChooseBook.class)) {
            if (c.getUserid() == uid && c.getBookid() == bid) {
                return false;
            }
        }
        return true;
    }

    private void initView(View view) {
        booklv = view.findViewById(R.id.booklv);
        bookList = LitePal.findAll(Book.class);
        adapter = new CollectionAdapter(getContext(), R.layout.collection_item, collectionItemList);
        booklv.setAdapter(adapter);
    }
}